class Solution {
public:
    string capitalizeTitle(string title) {
        string ans, temp;
        title += " ";
        bool f1 = 0;
        for (auto z : title) {
            if (z != ' ')temp += z;
            else {
                cout << "temp==" << temp << endl;
                if (temp.size() <= 2) {
                    for (auto& x : temp) {
                        if (x >= 'A' && x <= 'Z')x += 32;
                    }
                }
                else {
                    bool f = 0;
                    for (auto& x : temp) {
                        if (!f) {
                            f = 1;
                            if (x >= 'a' && x <= 'z')x -= 32;
                        }
                        else {
                            if (x >= 'A' && x <= 'Z')x += 32;
                        }
                    }
                }
                if (!f1) {
                    f1 = 1;
                    ans += temp;
                }
                else {
                    ans += " ";
                    ans += temp;
                }
                temp = "";
            }
        }
        return ans;
    }
};